package com.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.util.JpushClientUtil;

@Controller
@RequestMapping("jpush")
public class JpushController {
	private Logger log=Logger.getLogger(JpushController.class);
	
	/**
	 * 推送给设备标识用户
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("sendBymac.do")
	public void sendByMac(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		JSONObject json=new JSONObject();
		PrintWriter out = response.getWriter();
		String registrationId=request.getParameter("registrationId");
		String notification_title=request.getParameter("notification_title");
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		String extrasparam=request.getParameter("extrasparam");
//		extrasparam="0";
		if(title==null || "".equals(title)){
			title="";
		}
		if(content==null || "".equals(content)){
			content="";
		}
		if(extrasparam==null || "".equals(extrasparam)){
			extrasparam="";
		}
		log.info("入口参数---"+registrationId+"--"+notification_title+"--"+title+"--"+content+"--"+extrasparam);
		if(registrationId!=null && !"".equals(registrationId) && notification_title!=null && !"".equals(notification_title)){
				int result=JpushClientUtil.sendToRegistrationId(registrationId,notification_title,title,content,extrasparam);
				if(result==1){
					json.put("code", 1);
					json.put("msg", "推送成功");
				}else{
					json.put("code", 0);
					json.put("msg", "推送失败");
				}
		}else{
			json.put("code", -1);
			json.put("msg", "参数不能为空");
		}
		
		log.info("接口返回数据---"+json);
		out.print(json);
		out.flush();
		out.close();
	}
	
	
	/**
	 * 给所有安卓用户推送消息
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("sendAndroid.do")
	public void sendAndroid(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		JSONObject json=new JSONObject();
		PrintWriter out = response.getWriter();
		String notification_title=request.getParameter("notification_title");
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		String extrasparam=request.getParameter("extrasparam");
//		notification_title="通知";
//		title="安卓推送";
//		content="暑假优惠活动";
//		extrasparam="附加字段";
		if(title==null || "".equals(title)){
			title="";
		}
		if(content==null || "".equals(content)){
			content="";
		}
		if(extrasparam==null || "".equals(extrasparam)){
			extrasparam="";
		}
		if(notification_title!=null && !"".equals(notification_title)){
			int result=JpushClientUtil.sendToAllAndroid(notification_title, title, content, extrasparam);
			if(result==1){
				json.put("code", 1);
				json.put("msg", "推送成功");
			}else{
				json.put("code", 0);
				json.put("msg", "推送失败");
			}
			
		}else{
			json.put("code", -1);
			json.put("msg", "参数不能为空");
		}
		
		log.info("接口返回数据---"+json);
		out.print(json);
		out.flush();
		out.close();
	}
	
	
	/**
	 * 给所有的IOS用户推送消息
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("sendIos.do")
	public void sendIos(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		JSONObject json=new JSONObject();
		PrintWriter out = response.getWriter();
		String notification_title=request.getParameter("notification_title");
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		String extrasparam=request.getParameter("extrasparam");
		if(title==null || "".equals(title)){
			title="";
		}
		if(content==null || "".equals(content)){
			content="";
		}
		if(extrasparam==null || "".equals(extrasparam)){
			extrasparam="";
		}
		if(notification_title!=null && !"".equals(notification_title) ){
				
				int result=JpushClientUtil.sendToAllIos(notification_title, title, content, extrasparam);
				if(result==1){
					json.put("code", 1);
					json.put("msg", "推送成功");
				}else{
					json.put("code", 0);
					json.put("msg", "推送失败");
				}
		}else{
			json.put("code", -1);
			json.put("msg", "参数不能为空");
		}
		
		log.info("接口返回数据---"+json);
		out.print(json);
		out.flush();
		out.close();
	}
	
	
	/**
	 * 给所有用户推送消息
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("sendAlluser.do")
	public void sendAllUser(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		JSONObject json=new JSONObject();
		PrintWriter out = response.getWriter();
		String notification_title=request.getParameter("notification_title");
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		String extrasparam=request.getParameter("extrasparam");
		if(title==null || "".equals(title)){
			title="";
		}
		if(content==null || "".equals(content)){
			content="";
		}
		if(extrasparam==null || "".equals(extrasparam)){
			extrasparam="";
		}
		if(notification_title!=null && !"".equals(notification_title)){
				
				int result=JpushClientUtil.sendToAll(notification_title, title, content, extrasparam);
				if(result==1){
					json.put("code", 1);
					json.put("msg", "推送成功");
				}else{
					json.put("code", 0);
					json.put("msg", "推送失败");
				}
		}else{
			json.put("code", -1);
			json.put("msg", "参数不能为空");
		}
		
		log.info("接口返回数据---"+json);
		out.print(json);
		out.flush();
		out.close();
	}
}
